package com.lvmama.finance.demo.elasticsearch.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lvmama.finance.demo.elasticsearch.service.ProductService;
import com.lvmama.finance.demo.elasticsearch.vo.ProductVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@Api
@RequestMapping("/product")
public class ProductController {

	@Autowired
	private ProductService productService;
	
	@ApiOperation("find all products")
	@GetMapping
	public ResponseEntity<List<ProductVO>> findAllProducts() {
		return ResponseEntity.ok(productService.findAllProducts());
	}
	
	@ApiOperation("find one product")
	@GetMapping("/{productId}")
	public ResponseEntity<ProductVO> findOneProduct(@PathVariable("productId") long productId) {
		return ResponseEntity.ok(productService.findOneProduct(productId));
	}
	
	@ApiOperation("add a product")
	@PostMapping
	public ResponseEntity<ProductVO> addProduct(@RequestBody ProductVO product) {
		return ResponseEntity.ok(productService.addProduct(product));
	} 
	
	@ApiOperation("delete a product")
	@DeleteMapping("/{productId}")
	public void deleteProduct(@PathVariable("productId") long productId) {
		productService.deleteProduct(productId);
	}
}
